//
//  ContentManager.m
//  LeaveLight
//
//  Created by 张 on 2019/3/5.
//  Copyright © 2019年 xsl. All rights reserved.
//

#import "ContentManager.h"

#define TemplateSqliteName @"TemplateSqliteName"
#define saveTemplateKey @"saveTemplateKey"

@interface ContentManager ()

@property (strong, nonatomic) YYCache *cache;
@property (copy, nonatomic) NSArray *templateArrs;
@property (strong, nonatomic) NSString *documentFolder;

@end

@implementation ContentManager

DEF_SINGLETON(ContentManager)

- (instancetype)init {
    self = [super init];
    if (self) {
        self.documentFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
        NSString *path = [self.documentFolder stringByAppendingPathComponent:TemplateSqliteName];
        self.cache = [[YYCache alloc] initWithPath:path];
        _templateArrs = [[NSArray alloc] initWithArray:(NSArray *)[self.cache objectForKey:saveTemplateKey]];
        if (!_templateArrs) {
            _templateArrs = @[];
        }
    }
    return self;
}

- (TemplateModel *)addTemplateModelWith:(TemplateModel *)templateModel
{
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:self.templateArrs];
    
    if (!templateModel || ![templateModel isKindOfClass:[TemplateModel class]]) {
        return nil;
    }
    
    if (templateModel.isEditCar == NO) {
        templateModel.isEditCar = YES;
        [arr addObject:templateModel];
    }else{
        for (TemplateModel * model in arr) {
            if (templateModel.timestamp == model.timestamp) {
                [arr replaceObjectAtIndex:[arr indexOfObject:model] withObject:templateModel];
                break;
            }
        }
    }
    self.templateArrs = [[NSArray alloc] initWithArray:arr];
    [self updateTemplateStatus];
    return templateModel;
}

- (void)deleteTemplateWith:(TemplateModel *)deltemplate
{
    if (!deltemplate) {
        return;
    }
    NSMutableArray *arr = [[NSMutableArray alloc] initWithArray:self.templateArrs];
    if ([arr containsObject:deltemplate]) {
        [arr removeObject:deltemplate];
    }
    self.templateArrs = [[NSArray alloc] initWithArray:arr];
    [self updateTemplateStatus];
}

//- (void)deleteTemplateCacheWith:(TemplateModel *)deltemplate
//{
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
//        NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//        NSString *cachePath = [documentPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_cache",deltemplate.templatetName]];
//        YYDiskCache * cache = [[YYDiskCache alloc]initWithPath:cachePath];
//        [cache removeAllObjectsWithBlock:^{
//
//        }];
//    });
//    [self deleteTemplateWith:deltemplate];
//}

- (void)updateTemplateStatus
{
    [self.cache setObject:self.templateArrs forKey:saveTemplateKey withBlock:^{
        NSLog(@"updateTemplateStatus ok ");
    }];
}




@end







